/**
 * 省市级联
 */
// 如果为地市权限，进入页面就加载数据
$(function() {
	if (roleName == "t") {
		main();
	}
});

var provinceAndCity = (function() {
	function area(rdata) {
		var html = "";
		for (var i = 0; i < rdata.length; i++) {
			html += "<li><a class='province' href='#'>" + rdata[i]['item0'] + "</a></li>";
		}
		html += "<li><a class='province' href='#'>全国</a></li>";
		$(".provinceselect").html(html);
		getcity(province);// 级联地市
	}
	function area1(rdata){
    	var html="";
    	for(var i=0;i<rdata.length;i++){
    		html += "<li><a class='city' href='#'>"+rdata[i]['item0']+"</a></li>";
    	}
    	html += "<li><a class='city' href='#'>全省</a></li>";
    	//$("#cityName").text(rdata[0]['item0']);
		$(".cityselect").html(html);
		main();//省份权限，加载完地市再加载数据
    }

	return function() {
		var params = {};
		if (roleName == "c") {// 全国权限
			params = {
				"type" : "demo80"
			};
			params["province"] = "全国";
			BigData.Ajax("../../dataSource", "JSON", params, false, area);
		} else if (roleName == "p") {// 省份权限
			params = {
				"type" : "demo81"
			};
			params["param0"] = province;// 用户所在的省份
			params["province"] = "地市级联";
			BigData.Ajax("../../dataSource", "JSON", params, true, area1);
		}

	}
})();

//点击省份加载地市
var clickProvince = false;
$(document).on('click', '.province', function() {// 点击省份加载地市
	getcity(this.text);
	clickProvince = true;
	$(".provinceselect").slideUp("slow");
});

// 点击地市
$(document).on('click', '.city', function() {
	$("#cityName").text(this.text);
	$(".cityselect").slideUp("slow");
});

/*
 * $(document).on('click', '.sel-list-con>li', function() { var T =
 * $(this).parent().siblings(), id = T.prop("id"); T.text($(this).text());
 * $(this).parent().slideToggle(500); });
 */

// 加载地市方法
function getcity(province) {// 加载地市方法
	$("#provinceName").text(province);
	// var province1 = $("#provinceName").text();
	var params = {};
	params["type"] = "demo81";
	params["param0"] = province;
	params["province"] = "地市级联";

	/*
	 * if (province1 == '') {// 如果省份没有选择默认为用户所在省份 params["param0"] = province; }
	 * else {// 否则取省份选择 params["param0"] = province1; }
	 */

	$.ajax({
		async : true,
		url : "../../dataSource",
		type : "post",
		datatype : "JSON",
		data : params,
		success : function(rdata) {
			var html = "";
			for (var i = 0; i < rdata.length; i++) {
				html += "<li><a class='city' href='#'>" + rdata[i]['item0'] + "</a></li>";
			}

			if ($("#provinceName").text() != '全国') {// 如果省份不等于全国加载地市数据
				html += "<li><a class='city' href='#'>全省</a></li>";
			} else {// 否则地市数据为空
				$("#cityName").text("");
			}

			// 点击省份级联地市变换
			if (clickProvince) {
				$("#cityName").text(rdata[0]['item0']);
			}

			$(".cityselect").html(html);
			main();// 如果是全国权限，先加载省份地市再加载数据
		}
	});
}
